// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/05/15 14:15:56
// ==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace AutoChess
{
    public enum GroupPanelState
    {
        None,
        HasNotActive,
        HasActive
    }

    public class ACGroupPanel : MonoBehaviour
    {
        public AutoChessMainPage mainPage;
        public ACGroupBuffDetailPanel DetailPanel;
        public ACGroupItem Prefab;
        public Transform Content;
        public GameObject ShowBtn;
        public Image BGImage;

        private Dictionary<int, ACGroupItem> groupItemDict;
        private List<ACGroupItem> sortedItemList;
        private GroupPanelState panelState;
        private bool isClicked;

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="heroUnits"></param>
        public void SetData(List<int> heroUnits)
        {
            if (groupItemDict == null)
                InitGroups();

            // 初始化数据
            ResetData();
            foreach (var item in heroUnits)
            {
                var tableChessHero = Table.Blo.AutoChessBlo.GetChessHero(item);
                var groups = tableChessHero.acHeroGroups.Split(';');
                foreach (var group in groups)
                {
                    var groupId = int.Parse(group);
                    if (groupItemDict.ContainsKey(groupId))
                    {
                        groupItemDict[groupId].AddChess(item);
                        if (panelState == GroupPanelState.None)
                            panelState = GroupPanelState.HasNotActive;
                        if (panelState == GroupPanelState.HasNotActive && groupItemDict[groupId].IsActive)
                            panelState = GroupPanelState.HasActive;
                    }
                }
            }
            SortedGroupItems();
            RefreshPanelDisplay();
        }

        private void RefreshPanelDisplay()
        {
            ShowBtn.SetActive(panelState == GroupPanelState.HasNotActive && !isClicked);
            if(groupItemDict !=null)
            {
                foreach (var item in groupItemDict.Values)
                {
                    item.UIRefresh(isClicked);
                }
            }
            BGImage.enabled = isClicked;
        }

        private void SortedGroupItems()
        {
            for (int i = 0; i < sortedItemList.Count; i++)
            {
                for (int j = 0; j < sortedItemList.Count - i - 1; j++)
                {
                    var l = sortedItemList[j];
                    var r = sortedItemList[j + 1];
                    if (!l.IsActive && r.IsActive)
                    {
                        var temp = sortedItemList[j];
                        sortedItemList[j] = sortedItemList[j + 1];
                        sortedItemList[j + 1] = temp;
                    }
                }
            }

            for (int i = 0; i < sortedItemList.Count; i++)
            {
                sortedItemList[i].transform.SetSiblingIndex(i);
            }
        }

        private void InitGroups()
        {
            var allGroups = Table.Blo.AutoChessBlo.GetAllGroups();
            groupItemDict = new Dictionary<int, ACGroupItem>();
            sortedItemList = new List<ACGroupItem>();
            for (int i = 0; i < allGroups.Count; i++)
            {
                var group = allGroups[i];
                if (!groupItemDict.ContainsKey(group.acHeroGroup))
                {
                    var obj = Instantiate<ACGroupItem>(Prefab, Content);
                    obj.Init(this, group);
                    groupItemDict[group.acHeroGroup] = obj;
                    sortedItemList.Add(obj);
                }
            }
        }

        private void ResetData()
        {
            foreach (var item in groupItemDict.Values)
            {
                item.ClearChess();
            }
            isClicked = false;
            panelState = GroupPanelState.None;
            AutoChessManager.Ins.SetSameBuffEffect(false);
        }

        public void OnSelect(int acHeroGroup)
        {
            if(groupItemDict!=null)
            {
                if (acHeroGroup != 0)
                {
                    DetailPanel.SetData(acHeroGroup, groupItemDict[acHeroGroup].GetCurChessNum());
                    mainPage.SwitchCurPanel(ACMainPagePanelType.BuffInfo);
                    isClicked = true;
                    RefreshPanelDisplay();
                }
                else
                {
                    DetailPanel.Close();
                    isClicked = false;
                    RefreshPanelDisplay();
                }
                foreach (var item in groupItemDict)
                {
                    item.Value.SetSelectedState(item.Key == acHeroGroup);
                }
            }
        }

        public void OnSelectShowBtn()
        {
            isClicked = !isClicked;
            RefreshPanelDisplay();
        }

        public void Close()
        {
            isClicked = false;
            DetailPanel.Close();
            RefreshPanelDisplay();
            if(groupItemDict != null)
            {
                foreach (var item in groupItemDict)
                {
                    item.Value.SetSelectedState(false);
                }
            }
        }
    }
}

